package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import com.sandking.db.cache.YhbdrzCache;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 用户变动日志
 */
public class Yhbdrz {

	public static final String TABLENAME = "用户变动日志";
	public static final String CLASSNAME = "Yhbdrz"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 类型 */
	private String lx;
	
	/** 变动前 */
	private String bdq;
	
	/** 变动后 */
	private String bdh;
	
	/** 备注 */
	private String bz;
	
	
	public Yhbdrz() {
		super();
	}
	
	public Yhbdrz(int id, String lx, String bdq, String bdh, String bz) {
		super();
		this.id = id;
		this.lx = lx;
		this.bdq = bdq;
		this.bdh = bdh;
		this.bz = bz;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public String getLx() {
		return lx;
	}
	
	public void setLx(String lx) {
		this.lx = lx;
		addUpdateColumn("类型",lx);
	}
	
	public String getBdq() {
		return bdq;
	}
	
	public void setBdq(String bdq) {
		this.bdq = bdq;
		addUpdateColumn("变动前",bdq);
	}
	
	public String getBdh() {
		return bdh;
	}
	
	public void setBdh(String bdh) {
		this.bdh = bdh;
		addUpdateColumn("变动后",bdh);
	}
	
	public String getBz() {
		return bz;
	}
	
	public void setBz(String bz) {
		this.bz = bz;
		addUpdateColumn("备注",bz);
	}
	
	
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("lx", this.lx);
        result.put("bdq", this.bdq);
        result.put("bdh", this.bdh);
        result.put("bz", this.bz);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("类型", this.lx);
        result.put("变动前", this.bdq);
        result.put("变动后", this.bdh);
        result.put("备注", this.bz);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeString(out,this.lx);
		    SK_OutputStream.writeString(out,this.bdq);
		    SK_OutputStream.writeString(out,this.bdh);
		    SK_OutputStream.writeString(out,this.bz);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Yhbdrz createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Yhbdrz yhbdrz = new Yhbdrz();
		    yhbdrz.id = SK_InputStream.readInt(in,null);
		    yhbdrz.lx = SK_InputStream.readString(in,null);
		    yhbdrz.bdq = SK_InputStream.readString(in,null);
		    yhbdrz.bdh = SK_InputStream.readString(in,null);
		    yhbdrz.bz = SK_InputStream.readString(in,null);
		    return yhbdrz;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhbdrz> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhbdrz> yhbdrzs = new ArrayList<Yhbdrz>();
		for (Map<String, Object> map : list) {
			yhbdrzs.add(createForColumnNameMap(map));
		}
		return yhbdrzs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhbdrz createForColumnNameMap(Map<String, Object> map){
    	Yhbdrz obj = new Yhbdrz();
	    obj.id = SK_Map.getInt("id", map);
	    obj.lx = SK_Map.getString("类型", map);
	    obj.bdq = SK_Map.getString("变动前", map);
	    obj.bdh = SK_Map.getString("变动后", map);
	    obj.bz = SK_Map.getString("备注", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhbdrz> createForList(List<Map<String, Object>> list){
    	List<Yhbdrz> yhbdrzs = new ArrayList<Yhbdrz>();
		for (Map<String, Object> map : list) {
			yhbdrzs.add(createForColumnNameMap(map));
		}
		return yhbdrzs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhbdrz createForMap(Map<String, Object> map){
    	Yhbdrz obj = new Yhbdrz();
	    obj.id = SK_Map.getInt("id", map);
	    obj.lx = SK_Map.getString("lx", map);
	    obj.bdq = SK_Map.getString("bdq", map);
	    obj.bdh = SK_Map.getString("bdh", map);
	    obj.bz = SK_Map.getString("bz", map);
        return obj;
    }
    
    public static Yhbdrz createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhbdrz> createForJson(List<String> jsons){
    	List<Yhbdrz> yhbdrzs = new ArrayList<Yhbdrz>();
    	for(String json : jsons){
    		yhbdrzs.add(createForJson(json));
    	}
    	return yhbdrzs;
    }
    
    /** 延迟插入数据库 */
    public Yhbdrz insert(){
    	return YhbdrzCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhbdrz update(){
    	return YhbdrzCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhbdrzCache.delete(this);
    }
    /** 即时插入数据库 */
    public Yhbdrz insertAndFlush(){
    	return YhbdrzCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhbdrz updateAndFlush(){
    	return YhbdrzCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhbdrzCache.deleteAndFlush(this);
    }
}